package util.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;

import java.util.Iterator;

import java.util.regex.Pattern;

public class TextFileLoader implements Iterator<String[]>  {

	
	private String path = null;
	private String regex_split="\t";
	private boolean isDirectory=false;
	private File directory =null;
	private String[] list1 = null;
	private int  current_file_index=0;
	BigFile in = null;
	Iterator<String> iter= null;
	
	
	//Scanner current_scan =null;
	BigFile current_bigfile = null;
	Iterator<String> current_iterator= null;
	private Pattern p = null;
	
	
	
	public TextFileLoader(String path){

		

		
		this.path=path;
		directory = new File(path);
		list1 = directory.list();
	
		p = Pattern.compile(regex_split);
		if(directory.isDirectory()){
			isDirectory=true;
		
			if(current_file_index<list1.length){
				
				String current_file =list1[current_file_index];
			
				try {
					current_bigfile= new BigFile(path+"/"+current_file);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				current_iterator= current_bigfile.iterator();
				
				
				
				current_file_index++;
			}
		}else{
			
			try {
				in =new BigFile(path);
				iter =in.iterator();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	

	}
	
	public TextFileLoader(String path, String regex_split){
		

		this.path=path;
		directory = new File(path);
		list1 = directory.list();
		this.regex_split=regex_split;
		p = Pattern.compile(regex_split);
		if(directory.isDirectory()){
			isDirectory=true;
		
			if(current_file_index<list1.length){
				
				String current_file =list1[current_file_index];
			
				try {
					current_bigfile= new BigFile(path+"/"+current_file);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				current_iterator= current_bigfile.iterator();
				current_file_index++;
			}
		}else{
			
			try {
				in =new BigFile(path);
				iter =in.iterator();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		

	}
	
	
	


	

	@Override
	public boolean hasNext() {
		// TODO Auto-generated method stub
		
	
	
		if(isDirectory){
			String current_file=null;
		
			if(!current_iterator.hasNext()){
				
				if(current_file_index<list1.length){
				current_file =list1[current_file_index];
				current_file_index++;
				
				try {
					current_bigfile= new BigFile(path+"/"+current_file);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				current_iterator= current_bigfile.iterator();
				return current_iterator.hasNext();
				//FileReader fr = new FileReader (path+"/"+current_file);
				//BufferedReader br = new BufferedReader(fr);	
				//current_scan = new Scanner(br); 
				}else{
					
				
					return false;
				}
				
			}else{
				
				return true;
			}
			
			
			
			
			
			
			
		}else{
			
			return iter.hasNext();
			
		}
	}

	@Override
	public String[] next() {
		
	String next_line=null;
		if(isDirectory){
		try {
			next_line= readNextLineInDirectory();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}else{
			next_line= readNextLineInFile();
		}
		
		
		String[] temp = p.split(next_line);
		next_line=null;
		return temp;
		
		// TODO Auto-generated method stub
		
		
	}

	private String readNextLineInFile() {
		// TODO Auto-generated method stub
		
		
		String line = iter.next();
		
		
		
		
		return line;
	}

	private String readNextLineInDirectory() throws FileNotFoundException {
		// TODO Auto-generated method stub
		
		
		String line = current_iterator.next();
			
		return line;
		
	
	}

	@Override
	public void remove() {
		// TODO Auto-generated method stub
		
	}
	
	
	
	public void close(){
		
		if(isDirectory){
			
			in.Close();
		}else{
		 current_bigfile.Close();	
			
		}
		
	}
	
	public static void main(String arg[]){
		
		String path="/Users/sergioduarte/prueba/test1.txt";
		TextFileLoader t= new TextFileLoader(path);
		
		while(t.hasNext()){
			System.out.println(t.next()[0]);
			
		}
	}
	
}
